package com.xgp.labblog.service.Impl;

import com.xgp.labblog.mapper.WritesMapper;
import com.xgp.labblog.mapper.WritesMapperExt;
import com.xgp.labblog.model.Writes;
import com.xgp.labblog.model.WritesExample;
import com.xgp.labblog.service.WriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WriteServiceImpl implements WriteService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    WritesMapper writesMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    WritesMapperExt writesMapperExt;

    @Override
    public List<Writes> FindWriteByTime(Integer n) {
        WritesExample writeExample = new WritesExample();
        writeExample.setOrderByClause("time desc");
        List<Writes> writes = writesMapper.selectByExample(writeExample);
        return writes;
    }

    @Override
    public void addSupCount(Integer id) {
        writesMapperExt.addSupCountById(id);
    }

    @Override
    public List<Writes> FindWriteRand(int i) {
        WritesExample writeExample = new WritesExample();
        writeExample.setOrderByClause("rand() limit " + i);
        List<Writes> writes = writesMapper.selectByExample(writeExample);
        return writes;
    }

    @Override
    public Writes FindWriteById(Integer id) {
        Writes write = writesMapper.selectByPrimaryKey(id);
        return write;
    }

    @Override
    public void addReadCount(Writes write) {
        writesMapper.updateByPrimaryKeySelective(write);
    }

    @Override
    public List<Writes> FindWriteByTimeAndTitle(int i, String title) {
        WritesExample writeExample = new WritesExample();
        writeExample.setOrderByClause("time desc");
        writeExample.createCriteria().andTitleLike("%" + title + "%");
        List<Writes> writes = writesMapper.selectByExample(writeExample);
        return writes;
    }

}
